package com.tcc.fastline.DAO;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import android.content.Context;
import android.os.AsyncTask;
import android.renderscript.Type;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.tcc.fastline.DAO.interfaces.IFilaDAO;
import com.tcc.fastline.Domain.Estabelecimento;
import com.tcc.fastline.Domain.Fila;
import com.tcc.fastline.Domain.IntegranteFila;
import com.tcc.fastline.Domain.Usuario;
import com.tcc.fastline.Exceptions.ValidationServerException;
import com.tcc.fastline.HttpMethods.ExecuteTask;
import com.tcc.fastline.HttpMethods.HttpGetTask;
import com.tcc.fastline.HttpMethods.HttpPostTask;
import com.tcc.fastline.HttpMethods.Urls;

public class FilaDAO implements IFilaDAO
{

	private Gson gson;
	private Context contexto;

	public FilaDAO(Context context) {
		this.contexto = context;
		this.gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create();
	}

	@Override
	public List<Fila> ListarFilasPorEstabelecimento(Integer codigoEstabelecimento)
			throws ValidationServerException, Exception
	{
		List<Fila> filaObtida = new ArrayList<Fila>();
		try
		{
			String url = Urls.URL_OBTER_FILAS_POR_ESTABELECIMENTO + codigoEstabelecimento;

			String retorno = ExecuteTask.Execute(new HttpGetTask(url), contexto);

			filaObtida = gson.fromJson(retorno, new com.google.gson.reflect.TypeToken<List<Fila>>()
			{
			}.getType());

		}
		catch (ValidationServerException e)
		{
			e.printStackTrace();
			throw e;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			Log.e("Erro Exception", e.getMessage());
			throw new RuntimeException(e);
		}

		return filaObtida;
	}

	@Override
	public Fila ObterFilaPorUsuario(Integer codigoUsuario) throws ValidationServerException, Exception
	{
		Fila filaObtida = new Fila();
		try
		{
			String url = Urls.URL_OBTER_FILA_POR_USUARIO + codigoUsuario;

			String retorno = ExecuteTask.Execute(new HttpGetTask(url), contexto);

			if (retorno.equals("null"))
			{
				return null;
			}
			else
			{
				filaObtida = new Fila(retorno);
			}

		}
		catch (ValidationServerException e)
		{
			e.printStackTrace();
			throw e;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			Log.e("Erro Exception", e.getMessage());
			throw new RuntimeException(e);
		}

		return filaObtida;
	}

	@Override
	public void AdicionarUsuarioFila(IntegranteFila integrante) throws ValidationServerException, Exception
	{
		try
		{
			String url = Urls.URL_ADICIONAR_INTEGRANTE_FILA;

			String integranteJSON = integrante.toString();

			String retorno = ExecuteTask.Execute(new HttpPostTask(url, integranteJSON), contexto);

		}
		catch (ValidationServerException e)
		{
			e.printStackTrace();
			throw e;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			Log.e("Erro Exception", e.getMessage());
			throw new RuntimeException(e);
		}
	}

	@Override
	public Fila ObterFilaPorEstabelecimento(Integer codigoEstabelecimento) throws ValidationServerException,
			Exception
	{
		Fila filaObtida = new Fila();
		try
		{
			String url = Urls.URL_OBTER_FILA_POR_ESTABELECIMENTO + codigoEstabelecimento;

			String retorno = ExecuteTask.Execute(new HttpGetTask(url), contexto);

			filaObtida = new Fila(retorno);

		}
		catch (ValidationServerException e)
		{
			e.printStackTrace();
			throw e;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			Log.e("Erro Exception", e.getMessage());
			throw new RuntimeException(e);
		}

		return filaObtida;
	}
}
